<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号" clearable></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="物资编号" prop="suppliesCode">
						<el-input v-model="form.suppliesCode" placeholder="物资编号" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="物资名称" prop="suppliesName">
						<el-input v-model="form.suppliesName" placeholder="物资名称" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="所属分类" prop="sortId">
						<el-cascader v-model="form.sortId" :options="officeSuppliesSort" :props="officeSuppliesSortProps" :show-all-levels="false" clearable></el-cascader>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="数量" prop="quantity">
						<el-input-number v-model="form.quantity" placeholder="数量" clearable style="width: 100%;"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="计量单位" prop="unit">
						<sc-select v-model="form.unit" :params="{code:'unit'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="物资品牌" prop="brand">
						<el-input v-model="form.brand" placeholder="物资品牌" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="物资型号" prop="model">
						<el-input v-model="form.model" placeholder="物资型号" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="领用部门">
				<select-dept-dialog v-model:deptId="form.ownDept" :mode="true"/>
			</el-form-item>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" type="textarea" placeholder="备注说明" maxlength="180" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectDeptDialog from "@/views/module/selectDeptDialog";
export default {
	emits: ['success', 'closed'],
	components: {
		selectDeptDialog,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '物资录入对话框',
				edit: '编辑物资对话框',
			},
			officeSuppliesSort: [],
			officeSuppliesSortProps: {
				value: "sortId",
				label: "sortName",
				emitPath: false,
				checkStrictly: true
			},
			form: {
				suppliesId: '',
				sortNo: 0,
				suppliesName: '',
				suppliesCode: '',
				sortId: '',
				brand:'',
				model:'',
				quantity:0,
				unit:'',
				ownDept:'',
				remark: '',
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				suppliesName: [
					{required: true, message: '物资名称不能为空', trigger: 'blur'}
				],
				sortId: [
					{required: true, message: '物资分类不能为空', trigger: 'blur'}
				],
				quantity: [
					{required: true, message: '物资数量不能为空', trigger: 'blur'}
				],
				unit: [
					{required: true, message: '请选择计量单位', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getOfficeSuppliesSortGroup();
	},
	methods: {
		async getOfficeSuppliesSortGroup() {
			var res = await this.$API.office.officeSupplies.getOfficeSuppliesSortTree.get();
			this.officeSuppliesSort = res.data
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.officeSupplies.insertOfficeSupplies.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.officeSupplies.updateOfficeSupplies.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.suppliesId = data.suppliesId
			this.form.sortNo = data.sortNo
			this.form.suppliesName = data.suppliesName
			this.form.suppliesCode = data.suppliesCode
			this.form.sortId = data.sortId
			this.form.brand = data.brand
			this.form.quantity = data.quantity
			this.form.unit = data.unit
			this.form.model = data.model
			this.form.ownDept = data.ownDept
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
